package cn.com.haopy.dataes.es.task;


import cn.com.haopy.dataes.db.entity.erp.ErpGoods;
import cn.com.haopy.dataes.db.service.erp.ErpGoodsService;
import cn.com.haopy.dataes.db.utils.HttpUtils;
import cn.com.haopy.dataes.es.dto.DrugStockReqDto;
import cn.com.haopy.dataes.es.service.StoreCodeMatchCodeService;
import cn.com.haopy.dataes.es.utils.StockNumUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.Objects;


/**
 * @author keel
 * @date 2022/4/25
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class StockNumScheduledService {

    private static final Log logger = LogFactory.getLog(StockNumScheduledService.class);

    private final SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");

    @Autowired
    private ErpGoodsService erpGoodsService;

    @Autowired
    private StoreCodeMatchCodeService storeCodeMatchCodeService;

    /**
     * 需定时刷新，
     * 每个5分钟同步一次库存一次任务(方法) ,启动执行一次
     *
     * @return
     */
    @Scheduled(cron = "0 0/5 * * * ?")
    @PostConstruct
    public void getCustomer() {
        Integer limit = 10000;
        log.info("同步erp调拨单库存到互联网医院平台-----------开始---------");

        List<ErpGoods> list = erpGoodsService.queryLimit(limit);

        for (ErpGoods erpGood : list) {
            try {
                if (!erpGood.getGoodsQty().equals(0)) {
                    System.out.println(erpGood.getID());
                    DrugStockReqDto drugStockReqDto = new DrugStockReqDto();
                    drugStockReqDto.setInnerCode(erpGood.getGoodsId() + "");
                    drugStockReqDto.setNum(erpGood.getGoodsQty());
                    //医保码
                    if (Objects.nonNull(erpGood.getChsCode())) {
                        drugStockReqDto.setChsCode(erpGood.getChsCode().trim());
                    }


                    drugStockReqDto.setExpiryDate(dateFormatter.format(erpGood.getInvalidDate()));
                    drugStockReqDto.setPatchCode(erpGood.getLotNo());
                    String hosCode = storeCodeMatchCodeService.getHosCode(erpGood.getWareHouse());
                    //System.out.println("获取门店编码：" + hosCode);
                    drugStockReqDto.setStoreCode(hosCode);
                    if (Objects.nonNull(hosCode) && !StringUtils.hasText(hosCode)) {
                        erpGood.setInterfaceReason("仓库对码未找到");
                        erpGoodsService.updateById(erpGood);
                    } else {
                        Map<String, Object> params = StockNumUtil.toMap(drugStockReqDto);
                        System.out.println(params);
                        JSONObject data = HttpUtils.postJSON(StockNumUtil.addNumUrl, null, params);
                        System.out.println(data);
                        if (data.getInteger("errno").equals(0)) {
                            erpGood.setInterfaceReason("");
                            erpGood.setInterfaceFlag("Y");
                        } else {
                            erpGood.setInterfaceReason(data.getString("errmsg"));
                        }
                        erpGoodsService.updateById(erpGood);
                    }

                }


            } catch (Exception e) {
                log.error(e.toString());
                System.out.println(erpGood);
            }

        }


        log.info("同步erp调拨单库存到互联网医院平台-----------结束---------");

    }


}
